<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户添加</title>
    <%
        String basePath = request.getScheme() + "://" + request.getServerName() + ":"+
                request.getServerPort() +request.getContextPath()+ "/";
    %>
    <base href="<%=basePath%>">
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <script src="static/layui/layui.js"></script>
</head>
<body>
<br>
<br>
<form class="layui-form" action="">

    <div class="layui-form-item">
        <label class="layui-form-label">账号：</label>
        <div class="layui-input-inline">
            <input type="text" name="username" required  lay-verify="required" placeholder="请输入新的账号" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码：</label>
        <div class="layui-input-inline">
            <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">type：</label>
        <div class="layui-input-inline">
            <select name="type" lay-verify="required">
                <option value=""></option>
                <option value="0">管理员</option>
                <option value="1">普通用户</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">头像：</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <img style="width: 80px;height: 80px; border-radius: 50%" id="img">
        </div>
        <input type="hidden" name="imgName" id="userImg">
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script>
    //  var upload = layui.upload;
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        var $ = layui.$;
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.post("user.do?method=add",data.field,function (){

            });

            //延迟1秒执行
            window.setTimeout(function () {
                console.log("ok");
                //调用父页面的load方法
                parent.load("user.do?method=findPage");
                //通过父页面关闭当前页面
                var index=parent.layer.getFrameIndex(window.name);
                //先得到当前iframe层的索引
                parent.layer.close(index);
            })

            return false;//false取消提交，true执行提交
        });
    });
    layui.use('upload', function(){
        var upload = layui.upload;
        var $= layui.$;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: 'upload.do' //上传接口
            ,done: function(res){
                //上传完毕回调
                console.log(res);
                //图片回调
                $("#img").attr("src","http://localhost:8080/upload/"+res.data.src);
                //修改图片隐藏域值
                $("#userImg").val(res.data.src);
            }
            ,error: function(){
                //请求异常回调
                console.log("error");
            }
        });
    });
</script>
</html>
